/**
 ******************************************************************************
 * @file    newlib_stubs.cpp
 * @authors Matthew McGowan
 * @date    10 February 2015
 ******************************************************************************
  Copyright (c) 2015 Particle Industries, Inc.  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, either
  version 3 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************
 */

/* Define caddr_t as char* */
#include <sys/types.h>

/* Define abort() */
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"

void *__dso_handle;

extern "C" {

/*
 * Implement C++ new/delete operators using the heap
 */

void *operator new(size_t size)
{
	return malloc(size);
}

void *operator new[](size_t size)
{
	return malloc(size);
}

void operator delete(void *p)
{
	free(p);
}

void operator delete[](void *p)
{
	free(p);
}

void operator delete(void *p, size_t size)
{
	free(p);
}

void operator delete[](void *p, size_t size)
{
	free(p);
}

/* Bare metal, no processes, so error */
int _kill(int pid, int sig)
{
	return -1;
}

/* Bare metal, no processes, so always process id 1 */
int _getpid(void)
{
	return 1;
}

void _exit(int status) {
    PANIC(Exit,"Exit Called");

    while (1) {
        ;
    }
}

// Saves a few kB of flash.
char* strerror(int errnum) {
    return (char*)"";
}

// There is a bug in newlib in how it manages fake stdin/stdout/stderr
// and we'll leak memory. Stub it out
int __wrap_puts(const char* s) {
    return -1;
}

int __wrap_printf(const char* fmt, ...) {
    return -1;
}

/* Default implementation for call made to pure virtual function. */
void __cxa_pure_virtual() {
  PANIC(PureVirtualCall,"Call on pure virtual");
  while (1);
}

int __cxa_atexit(void (*f)(void *), void *p, void *d) {
    return 0;
}

void __register_exitproc(int type, void (*fn)(void), void* arg, void* d) {
}

}

namespace __gnu_cxx {

void __verbose_terminate_handler()
{
  abort();
}

}
